package com.cpr.shop.service.Impl;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teautil.models.RuntimeOptions;
import com.cpr.shop.service.SmsService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author CPR
 * @Create 2025/1/24 18:11
 * @Version 1.0
 * @Description
 */
@Service
@Slf4j
public class SmsServiceImpl implements SmsService {

    @Resource
    private Client smsClient;

    /**
     * 发送短信
     *
     * @param phoneNumber 手机号
     * @param code 验证码
     */
    public void sendMessage(String phoneNumber, String code) {
        log.info("手机号：{}",phoneNumber);
        SendSmsRequest sendSmsRequest = new SendSmsRequest();

        sendSmsRequest.setPhoneNumbers(phoneNumber);
        sendSmsRequest.setSignName("美妆商城毕设项目");
        sendSmsRequest.setTemplateCode("SMS_478420479");
        sendSmsRequest.setTemplateParam("{\"code\":\"" + code + "\"}");

        try {
            smsClient.sendSms(sendSmsRequest);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
